package com.zst.sw.server.module.segment;

import com.google.common.collect.Lists;
import com.zst.sw.server.module.span.SpanMapper;
import org.apache.skywalking.apm.network.language.agent.v3.SegmentObject;

import java.util.Collections;
import java.util.List;

public class SegmentMapper {
    public static List<Segment> map(List<SegmentObject> list) {
        if (list == null) {
            return Collections.emptyList();
        }

        List<Segment> ret = Lists.newArrayListWithExpectedSize(list.size());
        for (SegmentObject segmentObject : list) {
            ret.add(map(segmentObject));
        }
        return ret;
    }

    public static Segment map(SegmentObject segmentObject) {
        if (segmentObject == null) {
            return null;
        }

        Segment segment = new Segment();
        segment.setTraceId(segmentObject.getTraceId());
        segment.setTraceSegmentId(segmentObject.getTraceSegmentId());
        segment.setService(segmentObject.getService());
        segment.setServiceInstance(segmentObject.getServiceInstance());
        segment.setSpans(SpanMapper.map(segmentObject.getSpansList()));
        return segment;
    }

}
